/**
 * Copyright &copy; 2012-2016 < All rights reserved.
 */
package com.jeemicro.weixin.modules.cms.web.front;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.jeemicro.weixin.common.config.Global;
import com.jeemicro.weixin.common.persistence.Page;
import com.jeemicro.weixin.common.web.BaseController;
import com.jeemicro.weixin.modules.cms.entity.Baoming;
import com.jeemicro.weixin.modules.cms.entity.CmsHuiyuan;
import com.jeemicro.weixin.modules.cms.entity.Site;
import com.jeemicro.weixin.modules.cms.service.BaomingService;
import com.jeemicro.weixin.modules.cms.service.CmsHuiyuanService;
import com.jeemicro.weixin.modules.cms.utils.CmsUtils;

/**
 * 报名Controller
 * @author zmrid
 * @version 2017-07-15
 */
@Controller
@RequestMapping(value = "${frontPath}/baoming")
public class FrontBaomingController extends BaseController{
	
	@Autowired
	private BaomingService baomingService;
/*	@Autowired
	private CmsHuiyuanService cmsHuiyuanService;*/
	/**
	 * 报名
	 */
	@RequestMapping(value = "", method=RequestMethod.GET)
	public String baoming(@RequestParam(required=false, defaultValue="1") Integer pageNo,
			@RequestParam(required=false, defaultValue="30") Integer pageSize, Model model) {
		Site site = CmsUtils.getSite(Site.defaultSiteId());
		model.addAttribute("site", site);
		
		Page<Baoming> page = new Page<Baoming>(pageNo, pageSize);
		Baoming baoming = new Baoming();
		baoming.setDelFlag(Baoming.DEL_FLAG_NORMAL);
		page = baomingService.findPage(page, baoming);
		model.addAttribute("page", page);
		/*List<CmsHuiyuan> cmshuiyuan = cmsHuiyuanService.findList(new CmsHuiyuan());
	    model.addAttribute("cmshuiyuan", cmshuiyuan);*/
		return "modules/cms/front/themes/"+site.getTheme()+"/frontBaoming";
	}
	
	/**
	 * 报名-保存报名信息
	 */
	@RequestMapping(value = "", method=RequestMethod.POST)
	public String baomingSave(Baoming baoming, String validateCode, HttpServletRequest request, HttpServletResponse response, RedirectAttributes redirectAttributes) {
				baoming.setIp(request.getRemoteAddr());
				baoming.setCreateDate(new Date());
				baoming.setDelFlag(Baoming.DEL_FLAG_AUDIT);
				try{
					baomingService.save(baoming);
					addMessage(redirectAttributes, "报名成功，谢谢！");
					}catch(Exception e){
						addMessage(redirectAttributes, "您已经参与了报名！");
					}
		return "redirect:"+Global.getFrontPath()+"/baoming";
	}
}
